package com.zoe.wan.android.example.common.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ItemHomeListBinding
import com.zoe.wan.android.example.repository.data.HomeListItemData

class HomeListAdapter : RecyclerView.Adapter<HomeListAdapter.HomeListViewHolder>() {


    private var dataList: List<HomeListItemData> = mutableListOf()

    private fun setData(list: List<HomeListItemData>) {
        if (list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class HomeListViewHolder(binding: ItemHomeListBinding) : RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemHomeListBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeListViewHolder =
        HomeListViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context), R.layout.item_home_list, parent, false
            )
        )

    override fun getItemCount() = dataList.size

    override fun onBindViewHolder(holder: HomeListViewHolder, position: Int) {
        holder.itemBinding.item = dataList[position]
    }

}